package com.control.qct.businesssteward.activity;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.AccountDetailAdapter;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.fragment.AccountDateSelectFragment;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.model.AllAccountDetailModel;
import com.control.qct.businesssteward.model.StoreModel;
import com.control.qct.businesssteward.utils.DecimalFormatUtils;
import com.control.qct.businesssteward.utils.ScreenUtil;
import com.control.qct.businesssteward.view.MyItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 结算详情（日结/月结）
 * Created by Administrator on 2017/12/13 0013.
 */

public class AccountDetailActivity extends BaseActivity implements AccountDateSelectFragment.ICallbackDateListener {
    @BindView(R.id.tv_shop_name)
    TextView mTvShopName;//店铺名称
    @BindView(R.id.tv_account_time)
    TextView mTvAccountTime;//结算时间
    @BindView(R.id.tv_sale_count)
    TextView mTvSaleCount;//销售笔数
    @BindView(R.id.tv_sale_price)
    TextView mTvSalePrice;//销售金额
    @BindView(R.id.tv_complimentary_count)
    TextView mTvComplimentaryCount;//赠品笔数
    @BindView(R.id.tv_complimentary_price)
    TextView mTvComplimentaryPrice;//赠品金额
    @BindView(R.id.tv_return_goods_difference_count)
    TextView mTvReturnGoodsDifferenceCount;//退换货补价笔数
    @BindView(R.id.tv_return_goods_difference_price)
    TextView mTvReturnGoodsDifferencePrice;//退换货补价金额
    @BindView(R.id.tv_return_goods_count)
    TextView mTvReturnGoodsCount;//退换货退款笔数
    @BindView(R.id.tv_return_goods_price)
    TextView mTvReturnGoodsPrice;//退换货退款金额
    @BindView(R.id.tv_recharge_count)
    TextView mTvRechargeCount;//充值合计笔数
    @BindView(R.id.tv_recharge_price)
    TextView mTvRechargePrice;//充值合计金额
    @BindView(R.id.tv_recharge_revocation_count)
    TextView mTvRechargeRevocationCount;//充值撤销合计笔数
    @BindView(R.id.tv_recharge_revocation_price)
    TextView mTvRechargeRevocationPrice;//充值撤销合计金额
    @BindView(R.id.tv_stroke_count)
    TextView mTvStrokeCount;//结算合计笔数
    @BindView(R.id.tv_cash_price)
    TextView mTvCashPrice;//结算合计金额
    @BindView(R.id.rv_account_detail)
    RecyclerView mRvAccountDetail;//结算详情列表
    @BindView(R.id.nsv_account_detail)
    NestedScrollView mNsvAccountDetail;
    @BindView(R.id.fl_content)
    FrameLayout mFlContent;
    @BindView(R.id.dl_menu)
    public DrawerLayout mDlMenu;

    private Map<String, String> mParams;

    private List<AllAccountDetailModel.CashierSaleOrderListBean> mCashierSaleOrderListBeanList;
    private int mId;
    private String mStoreId;
    private String mSelectStoreId;
    private String mStoreName = "";
    private String mEndDate = "";
    private String mStartDate = "";
    private String mDate = "";
    private AccountDetailAdapter mAccountDetailAdapter;


    public AccountDetailActivity() {
        super(R.layout.activity_shop_account_detail);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        mId = extras.getInt("id");
        mStoreId = extras.getString("storeId");
        mSelectStoreId = mStoreId;
        mStoreName = extras.getString("storeName");
        mDate = extras.getString("date");
        mStartDate = extras.getString("startDate");
        mEndDate = extras.getString("endDate");
    }

    @Override
    protected void initView() {
        //关联侧滑菜单布局-- 日结/月结布局 按日期选择查看
        ViewGroup.LayoutParams params = mFlContent.getLayoutParams();
        params.width = (int) (ScreenUtil.getScreenWidthPix(this) * 0.85);
        mFlContent.setLayoutParams(params);
        replaceFragment(R.id.fl_content, new AccountDateSelectFragment(mId, this));
        //设置标题导航
        setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle(mId == 1 ? "门店日结" : "门店月结");
        setRightIcon("筛选", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDlMenu.openDrawer(mFlContent);
            }
        });
        //显示数据
        mTvShopName.setText(mStoreName);
        mTvAccountTime.setText(mId == 1 ? "日结时间：" + mDate : "月结时间：" + mStartDate + " - " + mEndDate);
        if (mId == 1) {
            getDayDetail();
        } else {
            getMonthDetail();
        }
        mCashierSaleOrderListBeanList = new ArrayList<>();
        mRvAccountDetail.setLayoutManager(new LinearLayoutManager(this));
        mRvAccountDetail.addItemDecoration(new MyItemDecoration());
        mAccountDetailAdapter = new AccountDetailAdapter(mCashierSaleOrderListBeanList);
        mRvAccountDetail.setAdapter(mAccountDetailAdapter);
    }


    /**
     * 获取门店日结明细
     */
    private void getDayDetail() {
        mParams = new HashMap<>();
        mParams.put("date", mDate);
        mParams.put("storeId", mSelectStoreId);
        mNetProtocol.getDayDetail(this, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                AllAccountDetailModel allAccountDetailModel = (AllAccountDetailModel) baseBean.getData();
                setData(allAccountDetailModel);
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }


    /**
     * 获取门店月结明细
     */
    private void getMonthDetail() {
        mParams = new HashMap<>();
        mParams.put("startDate", mStartDate);
        mParams.put("endDate", mEndDate);
        mParams.put("storeId", mSelectStoreId);
        mNetProtocol.getMonthDetail(this, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                AllAccountDetailModel allAccountDetailModel = (AllAccountDetailModel) baseBean.getData();
                setData(allAccountDetailModel);
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }


    /**
     * 显示数据
     *
     * @param allAccountDetailModel
     */
    private void setData(AllAccountDetailModel allAccountDetailModel) {
        mTvSaleCount.setText(allAccountDetailModel.getXsCount() + "");
        mTvSalePrice.setText(DecimalFormatUtils.DecimalFormatTwo(allAccountDetailModel.getXsMoney()));
        mTvComplimentaryCount.setText(allAccountDetailModel.getZsCount() + "");
        mTvComplimentaryPrice.setText(DecimalFormatUtils.DecimalFormatTwo(allAccountDetailModel.getZsMoney()));
        mTvReturnGoodsDifferenceCount.setText(allAccountDetailModel.getTuiHuanCount() + "");
        mTvReturnGoodsDifferencePrice.setText(DecimalFormatUtils.DecimalFormatTwo(allAccountDetailModel
                .getTuiHuanMoney()));
        mTvReturnGoodsCount.setText(allAccountDetailModel.getTuiCount() + "");
        mTvReturnGoodsPrice.setText(DecimalFormatUtils.DecimalFormatTwo(allAccountDetailModel.getTuiMoney()));
        mTvRechargeCount.setText(allAccountDetailModel.getCzCount() + "");
        mTvRechargePrice.setText(DecimalFormatUtils.DecimalFormatTwo(allAccountDetailModel.getCzMoney()));
        mTvRechargeRevocationCount.setText(allAccountDetailModel.getFczCount() + "");
        mTvRechargeRevocationPrice.setText(DecimalFormatUtils.DecimalFormatTwo(allAccountDetailModel
                .getFczMoney()));
        mTvStrokeCount.setText(allAccountDetailModel.getSsCount() + "");
        mTvCashPrice.setText("￥" + DecimalFormatUtils.DecimalFormatTwo(allAccountDetailModel.getSsMoney()));

        mCashierSaleOrderListBeanList.clear();
        mCashierSaleOrderListBeanList.addAll(allAccountDetailModel.getCashierSaleOrderList());
        if (mAccountDetailAdapter != null) {
            mAccountDetailAdapter.notifyDataSetChanged();
        }
    }


    @Override
    public void onBackPressed() {
        if (mDlMenu.isDrawerOpen(mFlContent)) {
            mDlMenu.closeDrawers();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onCallbackDate(String firstDate, String endDate, String storeId, String storeName) {
        mTvShopName.setText(TextUtils.isEmpty(storeName) ? mStoreName : storeName);
        mSelectStoreId = TextUtils.isEmpty(storeId) ? mStoreId : storeId;
        switch (mId) {
            case 1:
                mDate = firstDate;
                mTvAccountTime.setText("日结时间：" + mDate);
                getDayDetail();
                break;
            case 2:
                mStartDate = firstDate;
                mEndDate = endDate;
                mTvAccountTime.setText("月结时间：" + mStartDate + " - " + mEndDate);
                getMonthDetail();
                break;
        }
    }
}
